package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxShopSupplier;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.YxShopSupplierRepository;
import co.yixiang.modules.shop.service.YxShopSupplierService;
import co.yixiang.modules.shop.service.dto.YxShopSupplierDto;
import co.yixiang.modules.shop.service.dto.YxShopSupplierQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxShopSupplierMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "yxShopSupplier")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxShopSupplierServiceImpl implements YxShopSupplierService {

    @Resource
    private YxShopSupplierRepository yxShopSupplierRepository;
    @Resource
    private YxShopSupplierMapper yxShopSupplierMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxShopSupplierQueryCriteria criteria, Pageable pageable){
        Page<YxShopSupplier> page = yxShopSupplierRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxShopSupplierMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxShopSupplierDto> queryAll(YxShopSupplierQueryCriteria criteria){
        return yxShopSupplierMapper.toDto(yxShopSupplierRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxShopSupplierDto findById(Integer id) {
        YxShopSupplier yxShopSupplier = yxShopSupplierRepository.findById(id).orElseGet(YxShopSupplier::new);
        ValidationUtil.isNull(yxShopSupplier.getId(),"YxShopSupplier","id",id);
        return yxShopSupplierMapper.toDto(yxShopSupplier);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxShopSupplierDto create(YxShopSupplier resources) {
        return yxShopSupplierMapper.toDto(yxShopSupplierRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxShopSupplier resources) {
        YxShopSupplier yxShopSupplier = yxShopSupplierRepository.findById(resources.getId()).orElseGet(YxShopSupplier::new);
        ValidationUtil.isNull( yxShopSupplier.getId(),"YxShopSupplier","id",resources.getId());
        yxShopSupplier.copy(resources);
        yxShopSupplierRepository.save(yxShopSupplier);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxShopSupplierRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<YxShopSupplierDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxShopSupplierDto yxShopSupplier : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("供应商名称", yxShopSupplier.getName());
            map.put("公司名称", yxShopSupplier.getCompanyName());
            map.put("统一社会信用代码", yxShopSupplier.getCreditCode());
            map.put("状态（0：关闭，1：开启）", yxShopSupplier.getStatus());
            map.put("联系电话", yxShopSupplier.getContactPhone());
            map.put("联系人", yxShopSupplier.getContactPerson());
            map.put("添加时间", yxShopSupplier.getAddTime());
            map.put("改变时间", yxShopSupplier.getChangeTime());
            map.put("是否删除", yxShopSupplier.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}